
#include "pbm.h"

#include "xapp_notification.h"
#include "xapp_profile.h"
#include "xapp_record.h"
#include "xapp_system.h"

#include "xapp_task.h"

#include "xPLATFORM.h"

#if 1 // System
pbd_err_code_t pi_system_power_off(uint8_t *pdata, uint16_t size)
{
    if( size != 1){
        return PBD_ERR_INVALID_LENGTH;
    }
    if( pdata[0] != 'P'){
        return PBD_ERR_INVALID_PARAMS; 
    }

    xapp_system_set_mode( SYSTEM_MODE_POWER_OFF );
    return PBD_SUCCESS; 
}
pbd_err_code_t pi_system_device_reset(uint8_t *pdata, uint16_t size)
{
    if( size == 2 && pdata[0] == 'D' && pdata[1] == 'R')
    {
        SYSTEM_SET_RESET();
        return PBD_SUCCESS; 
    }

    if( size != 1){
        return PBD_ERR_INVALID_LENGTH;
    }
    if( pdata[0] != 'R'){
        return PBD_ERR_INVALID_PARAMS; 
    }

    xapp_system_set_mode( SYSTEM_MODE_RESET );

    return PBD_SUCCESS;
}
pbd_err_code_t pi_system_enter_dfu(uint8_t *pdata, uint16_t size)
{
    if( size != 1){
        return PBD_ERR_INVALID_LENGTH;
    }
    if( pdata[0] != 'D'){
        return PBD_ERR_INVALID_PARAMS;
    }

    return PBD_ERR_SUB_FUNCTION_NOT_SUPPORTED; 
}
pbd_err_code_t pi_system_enter_tpdfu(uint8_t *pdata, uint16_t size)
{
    if( size != 1){
        return PBD_ERR_INVALID_LENGTH;
    }
    if( pdata[0] != 'T'){
        return PBD_ERR_INVALID_PARAMS;
    }

	xapp_system_set_mode( SYSTEM_MODE_TPDFU );

    return PBD_SUCCESS; 
}

pbd_err_code_t pi_sytem_enter_ulp(uint8_t* pdata, uint16_t size)
{
    if( size != 1)
    {
        return PBD_ERR_SUB_FUNCTION_NOT_SUPPORTED;
    }
    if( pdata[0] != 'U')
    {
        return PBD_ERR_SUB_FUNCTION_NOT_SUPPORTED;
    }

    return PBD_ERR_SUB_FUNCTION_NOT_SUPPORTED; 
}

pbd_err_code_t pi_system_factory_reset(uint8_t *pdata, uint16_t size)
{
    if( size != 1)
    {
        return PBD_ERR_INVALID_LENGTH;
    }
    if(pdata[0] != 'F')
    {
        return PBD_ERR_INVALID_PARAMS; 
    }

    xapp_task_set_factory_reset();

    return PBD_SUCCESS;
}
pbd_err_code_t pi_system_clear_history_record(uint8_t *pdata, uint16_t size)
{
    if( size != 1){
        return PBD_ERR_INVALID_LENGTH;
    }

    if( pdata[0] != 'H'){
        return PBD_ERR_INVALID_PARAMS; 
    }

    xapp_task_set_erase_record();

    return PBD_SUCCESS;
}
pbd_err_code_t pi_system_reset_person_profile(uint8_t *pdata, uint16_t size)
{
    if( size != 1){
        return PBD_ERR_INVALID_LENGTH;
    }
    if( pdata[0] != 'C'){
        return PBD_ERR_INVALID_PARAMS; 
    }   

    xapp_task_set_profile_default();

    return PBD_SUCCESS;
}

pbd_err_code_t pi_system_set_mode(uint8_t *pdata, uint16_t size)
{
    if( size != 1){
        return PBD_ERR_INVALID_LENGTH;
    }

    return PBD_ERR_SUB_FUNCTION_NOT_SUPPORTED;
}

#endif // !System

